package com.start_eternity.oasissafewalk.controller;

import com.start_eternity.oasissafewalk.comment.ErrorCode;
import com.start_eternity.oasissafewalk.comment.ResultUtils;
import com.start_eternity.oasissafewalk.pojo.Driver;
import com.start_eternity.oasissafewalk.pojo.Notice;
import com.start_eternity.oasissafewalk.pojo.Order;
import com.start_eternity.oasissafewalk.pojo.PageBean;
import com.start_eternity.oasissafewalk.comment.Result;
import com.start_eternity.oasissafewalk.service.DriverService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/drivers")
public class DriverController {

    @Autowired
    private DriverService driverService;

    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10")Integer pageSize,
                       String driverName, Short gender,
                       @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern="yyyy-MM-dd")LocalDate end){
        log.info("分页查询,参数:{},{},{},{},{},{}",page,pageSize,driverName,gender,begin,end);
        //调用service方法进行分页查询
        PageBean pageBean= driverService.page(page,pageSize,driverName, gender, begin,end);
        return Result.success(pageBean);
    }
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除操作,ids:{}",ids);
        driverService.delete(ids);
        return Result.success();
    }
    @PostMapping
    public Result save(@RequestBody Driver driver){
        log.info("新增司机,driver:{}",driver);
        driverService.save(driver);
        return Result.success();
    }
    @GetMapping("/{driverId}")
    public Result getById(@PathVariable Integer driverId){
        log.info("根据ID查询司机信息,:{}",driverId);
        Driver driver=driverService.getById(driverId);
        return Result.success(driver);
    }
    @PutMapping
    public Result update(@RequestBody Driver driver){
        log.info("更新司机信息:{}",driver);
        driverService.update(driver);
        return Result.success();
    }
    //  查询司机关于订单
    @GetMapping("/queryListByPId")
    public Result queryListByPId(@RequestParam Integer driverId) {
        List<Driver> drivers = driverService.queryListByPId(driverId);
        if (!drivers.isEmpty()) {
            return Result.success(drivers);
        }
        return Result.error("未查询到该账户关联的订单信息");
    }
    /*查询通知信息*/
    @GetMapping("/message/{driverId}")
    public Result getMessageById(@PathVariable Integer driverId) {
        List<Driver> drivers = driverService.getMessageById(driverId);
        if (!drivers.isEmpty()) {
            return Result.success(drivers);
        }
        return Result.error("未查询到该账户关联的通知信息");
    }

    /**
     * 发送送达消息
     * @param notice
     * @return
     */
    @PostMapping("/send")
    public Result saveNotice(@RequestBody Notice notice){
        log.info("新增消息,notice:{}",notice);
        driverService.addNotice(notice);
        return Result.success();
    }
}
